package com.manydatabase.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration
@MapperScan(basePackages = "com.gq.boot.manydatabase.mapper.mapper1",
        sqlSessionFactoryRef = "primarySqlSessionFactory",
        sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class DataSource1Config {
    private static final Logger Log = LoggerFactory.getLogger(DataSource1Config.class);

    @Value("${mybatis.primary.mapper-locations}")
    private String mapperXmlLocations;


    @Bean(name = "DataSource1")
    @ConfigurationProperties(prefix = "primary.datasource.druid")
    @Primary
    public DataSource getDataSource1() {
        DruidDataSource dataSource = DataSourceBuilder.create().type(DruidDataSource.class).build();
        if (dataSource == null) {
            Log.error("数据源PrimaryDataSource初始化失败！！！！！");
        } else {
            Log.info("数据源PrimaryDataSource初始化完成！！！！！");
        }
        return dataSource;
    }


    @Bean("primarySqlSessionFactory")
    public SqlSessionFactory getSqlSessionFactory(@Qualifier("DataSource1") DataSource dataSource1) throws IOException {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapperXmlLocations));
        sqlSessionFactoryBean.setDataSource(dataSource1);
        SqlSessionFactory factory = null;
        try {
            factory = sqlSessionFactoryBean.getObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (factory == null) {
            Log.error("primaryJdbcTemplate装配失败！！！！");
        } else {
            Log.info("primaryJdbcTemplate装配完成！！！！");
        }
        return factory;
    }

    @Bean(name = "primarySqlSessionTemplate")
    public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory factory) {
        SqlSessionTemplate template = new SqlSessionTemplate(factory);
        Log.info("primarySqlSessionTemplate装配完成！！！！");
        return template;
    }

    @Bean(name = "primaryTransactionManager")
    public DataSourceTransactionManager getTransactionManager(@Qualifier("DataSource1") DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
        Log.info("primaryTransactionManager事务管理器初始化完成！！！！");
        return transactionManager;
    }


}
